package cn.elwy.rcp.ui.explorer.core.service.impl;

import java.io.File;

import org.springframework.stereotype.Service;
import org.tmatesoft.svn.core.SVNException;

import cn.elwy.common.exception.RunException;
import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.explorer.core.handler.PackerHandler;
import cn.elwy.rcp.ui.explorer.core.service.PackerService;
import cn.elwy.rcp.ui.explorer.core.util.SVNClient;
import cn.elwy.rcp.ui.explorer.core.util.SvnUtil;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;

/**
 * 打包服务类
 * 
 * @author huangsq
 * @version 1.0, 2014-09-22
 * @since 1.0, 2014-09-22
 */
@Service(PackerService.ID)
public class PackerServiceImpl implements PackerService {

  public void run(ProjectConfig projectConfig, ProgressMonitor monitor) {
    SVNClient svnClient = null;
    try {
      svnClient = SvnUtil.getClientManager(projectConfig.getScmUrl(), projectConfig.getScmUser(),
          projectConfig.getScmPassword());
      PackerHandler packer = new PackerHandler();
      packer.setSvnClient(svnClient);

      packer.execute(projectConfig, monitor);

      if (projectConfig.isUpdateStartVersion()) {
        long revision = SvnUtil.getRevision(svnClient.getClientManager(), new File(projectConfig.getResourcePath(),
                projectConfig.getSourceDirectory()));
        projectConfig.setStartVersion("" + revision);
      }

    } catch (SVNException e) {
      throw new RunException(e.getMessage(), e);
    } finally {
      if (svnClient != null) {
        svnClient.dispose();
      }
    }
  }

}
